package com.gjr.demo;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.gjr.entity.User;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 呙境仁
 * @create 2021-07-19 18:09
 */
public class CodeGenerator {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Test
    public void main() {
        // 1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 2、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("gjr");
        gc.setOpen(false); //生成后是否打开资源管理器
        gc.setFileOverride(false); //重新生成时文件是否覆盖
        gc.setServiceName("%sService"); //去掉Service接口的首字母I
        gc.setIdType(IdType.ID_WORKER_STR); //主键策略
        gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
        gc.setSwagger2(false);//开启Swagger2模式

        mpg.setGlobalConfig(gc);

        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/zaixianjiaoyu?serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 4、包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(null); //模块名
        pc.setParent("com.gjr.eduservice");
        pc.setController("controller");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("edu_course","edu_course_description","edu_chapter","edu_video");//对那一张表生成代码
        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
        strategy.setTablePrefix(pc.getModuleName() + "_"); //生成实体时去掉表前缀

        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作

        strategy.setRestControllerStyle(true); //restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符

        mpg.setStrategy(strategy);

        // 6、执行
        mpg.execute();
    }

    @Test
    public void test() {
        HashMap<String, String> map = new HashMap<>();
        map.put("name", "cookie");
        map.put("age", "18");
        map.put("sex", "女");
        String name = map.getOrDefault("name", "random");
        System.out.println(name);// cookie，map中存在name,获得name对应的value
        String score = map.getOrDefault("score", "80");
        System.out.println(score);// 80，map中不存在score,使用默认值80
    }

    @Test
    public void mapTest() throws ParseException {
        List<User> list = new LinkedList<>();


        list.add(new User(1, 1, sdf.parse("2021-08-25 11:11:11")));
        list.add(new User(2, 1, sdf.parse("2021-08-24 11:11:11")));
        list.add(new User(3, 1, sdf.parse("2021-08-23 11:11:11")));

        list.add(new User(4, 2, sdf.parse("2021-07-20 11:11:11")));
        list.add(new User(5, 2, sdf.parse("2021-07-19 11:11:11")));
        list.add(new User(6, 2, sdf.parse("2021-07-18 11:11:11")));

        list.add(new User(7, 3, sdf.parse("2021-06-02 11:11:11")));
        list.add(new User(8, 3, sdf.parse("2021-06-05 11:11:11")));
        list.add(new User(9, 3, sdf.parse("2021-06-03 11:11:11")));

        long time = sdf.parse("2021-06-03 11:11:11").getTime();

        list.stream().collect(Collectors.groupingBy(store -> store.getConfigFormId())).forEach((key, value) -> {
            // 得到最大的时间值
            Collections.sort(value, (User o1, User o2) ->
                    (int) (o2.getCreatedAt().getTime() - o1.getCreatedAt().getTime())
            );

            //value.sort(Comparator.comparing(User::getCreatedAt).reversed());

            User user = value.get(0);
            Date maxCreatedAt = value.get(0).getCreatedAt();

            // 得到最小的时间值
            Collections.sort(value, (User o1, User o2) ->
                    (int) (o1.getCreatedAt().getTime() - o2.getCreatedAt().getTime())
            );

            //value.sort(Comparator.comparing(User::getCreatedAt).reversed().reversed());

            Date minCreatedAt = value.get(0).getCreatedAt();

            user.setMaxCreatedAt(maxCreatedAt);
            user.setMinCreatedAt(minCreatedAt);

            System.out.println("对象：" + user);
        });


        /*List<Map<String, Object>> collect = returnList.stream().filter(map -> !ObjectUtils.isEmpty(map.get("last_treatment"))).sorted(new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                //正序 o1 - o2
                return Integer.valueOf(o1.get("last_treatment").toString().replaceAll("-", "")) - Integer.valueOf(o2.get("last_treatment").toString().replaceAll("-", ""));
            }
        }).collect(Collectors.toList());

        collect.addAll(returnList.stream().filter(map -> ObjectUtils.isEmpty(map.get("last_treatment"))).collect(Collectors.toList()));*/
    }
}

